package com.yuteng.video.controller;


import com.yuteng.video.entity.Result;
import com.yuteng.video.entity.Search;
import com.yuteng.video.service.impl.SearchServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/search")
public class SearchController {
    @Autowired
    SearchServiceImpl searchService;
    @GetMapping("/tet")
    public Result tests(String s){
        return new Result(true, 2000, "添加成功");
    }

    /**
     * 保存search
     *
     * @param search
     */
    @PostMapping
    public Result saveSearch(@RequestBody Search search) {
        searchService.saveSearch(search);
        return new Result(true, 2000, "添加成功");
    }

    /**
     * 条件查询
     * @param msg
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping
    public Result selectSearchByTitleOrDescription(String msg,
                                                   @RequestParam(defaultValue = "1") int pageNum,
                                                   @RequestParam(defaultValue = "5") int pageSize) {
        Page list = searchService.selectSearchByTitleOrDescription(msg, pageNum, pageSize);
        List list1 = new ArrayList();
        list1.add(list);
        return new Result(true, 2000, "查询成功", list1);
    }

    /**
     * 查询所有
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getAll")
    public Result getAll(@RequestParam(defaultValue = "1") int pageNum,
                         @RequestParam(defaultValue = "5") int pageSize) {
        Page all = searchService.getAll(pageNum, pageSize);
        List list = new ArrayList();
        list.add(all);
        return new Result(true, 2000, "查询成功", list);
    }

    /**
     * 根据个人喜好查询
     * @param name
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getLike")
    public Result getLike(@RequestParam(defaultValue = "游客")String name, @RequestParam(defaultValue = "1") int pageNum,
                          @RequestParam(defaultValue = "5") int pageSize) {
        List like = searchService.getLike(name, pageNum, pageSize);
        return new Result(true, 2000, "查询成功",like);
    }
    @GetMapping("/getLikeFromRedis")
    public Map getLikeFromRedis(){
       return searchService.getLikeFromRedis("test");
    }
}
